<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page import="java.util.List, com.petcommunity.model.Adoption" %>
<%
    List<Adoption> adoptions = (List<Adoption>) request.getAttribute("adoptions");
%>
<%
    String role = (String) session.getAttribute("role");
%>
<html>
<head>
    <title>领养管理 - 社区宠物管理系统</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <link rel="stylesheet" href="css/management.css">
</head>
<body>
    <!-- 装饰性爪印 -->
    <i class="fas fa-paw decorative-paws paw-1"></i>
    <i class="fas fa-paw decorative-paws paw-2"></i>
    <i class="fas fa-paw decorative-paws paw-3"></i>
    <i class="fas fa-paw decorative-paws paw-4"></i>

    <div class="page-container">
        <div class="main-container">
            <!-- 页面头部 -->
            <div class="page-header">
                <div class="header-content">
                    <div class="header-icon">
                        <i class="fas fa-heart"></i>
                    </div>
                    <h1 class="page-title">领养管理</h1>
                    <p class="page-subtitle">为每个小生命找到温暖的家</p>
                </div>
            </div>

            <div class="content-wrapper">
                <!-- 添加领养表单 -->
                <c:if test="${role == 'admin'}">
                    <div class="form-section">
                        <div class="section-header">
                            <div class="section-title">
                                <i class="fas fa-plus-circle section-icon"></i>
                                添加新领养记录
                            </div>
                        </div>
                        <form method="post" action="adoptions" class="add-form">
                            <div class="form-row">
                                <div class="form-group">
                                    <label>
                                        <i class="fas fa-dog"></i>
                                        宠物ID
                                    </label>
                                    <input type="number" name="petId" required placeholder="请输入宠物ID">
                                </div>
                                <div class="form-group">
                                    <label>
                                        <i class="fas fa-user"></i>
                                        领养人姓名
                                    </label>
                                    <input type="text" name="adopterName" required placeholder="请输入领养人姓名">
                                </div>
                            </div>
                            <div class="form-row">
                                <div class="form-group">
                                    <label>
                                        <i class="fas fa-phone"></i>
                                        联系电话
                                    </label>
                                    <input type="text" name="adopterPhone" required placeholder="请输入联系电话">
                                </div>
                                <div class="form-group">
                                    <label>
                                        <i class="fas fa-calendar-alt"></i>
                                        领养日期
                                    </label>
                                    <input type="date" name="adoptionDate" required>
                                </div>
                            </div>
                            <div class="form-actions">
                                <button type="submit" class="submit-btn">
                                    <i class="fas fa-heart btn-icon"></i>
                                    添加领养记录
                                </button>
                            </div>
                        </form>
                    </div>
                </c:if>

                <!-- 领养记录表格 -->
                <div class="table-section">
                    <div class="section-header">
                        <div class="section-title">
                            <i class="fas fa-list section-icon"></i>
                            领养记录列表
                        </div>
                        <div class="table-info">
                            <span class="record-count">
                                共 ${adoptions != null ? adoptions.size() : 0} 条记录
                            </span>
                        </div>
                    </div>

                    <div class="table-container">
                        <c:choose>
                            <c:when test="${adoptions != null && adoptions.size() > 0}">
                                <table class="data-table">
                                    <thead>
                                        <tr>
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-hashtag"></i>
                                                    领养ID
                                                </div>
                                            </th>
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-dog"></i>
                                                    宠物信息
                                                </div>
                                            </th>
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-user"></i>
                                                    领养人信息
                                                </div>
                                            </th>
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-phone"></i>
                                                    联系方式
                                                </div>
                                            </th>
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-calendar"></i>
                                                    领养日期
                                                </div>
                                            </th>
                                            <c:if test="${role == 'admin'}">
                                            <th>
                                                <div class="th-content">
                                                    <i class="fas fa-cogs"></i>
                                                    操作
                                                </div>
                                            </th>
                                        </c:if>
                                        </tr>
                                    </thead>
                                    <tbody>
                                        <c:forEach var="a" items="${adoptions}">
                                            <tr>
                                                <td class="id-cell" data-label="领养ID">${a.adoptionId}</td>
                                                <td data-label="宠物信息">
                                                    <div class="pet-info">
                                                        <div class="pet-avatar">
                                                            <i class="fas fa-paw"></i>
                                                        </div>
                                                        <span class="owner-id">ID: ${a.petId}</span>
                                                    </div>
                                                </td>
                                                <td data-label="领养人信息">
                                                    <div class="user-info">
                                                        <div class="user-avatar">
                                                            ${a.adopterName.substring(0, 1)}
                                                        </div>
                                                        <span class="user-name">${a.adopterName}</span>
                                                    </div>
                                                </td>
                                                <td data-label="联系方式">
                                                    <a href="tel:${a.adopterPhone}" class="phone-link">
                                                        ${a.adopterPhone}
                                                    </a>
                                                </td>
                                                <td data-label="领养日期">
                                                    <span class="birth-date">${a.adoptionDate}</span>
                                                </td>
                                                <c:if test="${role == 'admin'}">
                                                <td data-label="操作">
                                                    <div class="action-buttons">
                                                        <button class="edit-btn" onclick="editAdoption(${a.adoptionId})">
                                                            <i class="fas fa-edit"></i>
                                                        </button>
                                                        <button class="delete-btn" onclick="deleteAdoption(${a.adoptionId})">
                                                            <i class="fas fa-trash"></i>
                                                        </button>
                                                    </div>
                                                </td>
                                            </c:if>
                                            </tr>
                                        </c:forEach>
                                    </tbody>
                                </table>
                            </c:when>
                            <c:otherwise>
                                <div class="empty-state">
                                    <div class="empty-icon">
                                        <i class="fas fa-heart-broken"></i>
                                    </div>
                                    <h3>暂无领养记录</h3>
                                    <p>目前还没有任何领养记录，期待第一个温暖的家庭</p>
                                </div>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>

                <!-- 返回按钮 -->
                <div class="back-section">
                    <a href="dashboard.jsp" class="back-btn">
                        <i class="fas fa-home"></i>
                        返回首页
                    </a>
                </div>
            </div>
        </div>
    </div>
<script>
function editAdoption(id) {
    if (confirm('确定要编辑该领养记录吗？')) {
        window.location.href = 'edit?type=adoption&id=' + id;
    }
}

function deleteAdoption(id) {
    if (confirm('确定要删除该领养记录吗？此操作不可恢复！')) {
        window.location.href = 'delete?type=adoption&id=' + id;
    }
}
</script>

</body>
</html>